// ==UserScript==
// @name           Shack Category Banners
// @namespace      http://www.apartment167.com/overload/greasemonkey
// @description    Adds "NWS" style banners to other types of posts.
// @include http://shacknews.com/laryn.x?*
// @include http://*.shacknews.com/laryn.x?*
// ==/UserScript==

/*
	Shack Category Banners
	Author: Greg Laabs "OverloadUT"
	(C)2008 Greg Laabs

	REVISIONS:

	2008-02-21
		- Initial Release
		
	2008-02-21
		- Added the images to the script file in base64, so it doesn't have to load off my servers
		
	2008-02-21
		- Changed the code so addGlobalStyle is only called once, as it's a slow function. Thanks ThomW!
    2009-05-16
        - ported to chrome
	2009-10-13
		- Updated for Chrome Extension 2.0 
		- Moved CSS to shackscripts.css and changed method for specifying which banners to display
		- Fixed ambiguity problems with true vs. "true" 

*/
if (script_enabled('category_banners') == "true") 
{

 (function() {

	var benchmarkTimer = null;
	var scriptStartTime = getTime();
	function getTime() { benchmarkTimer = new Date(); return benchmarkTimer.getTime(); }
	
	function toggleBanner() 
	{
		if (this.checked)
		{
			GM_setValue(this.id, 'true'); 
		}
		else
		{
			GM_setValue(this.id, 'false'); 
		}
	}
	
	if (GM_getValue("banner_nws", 'poop') == 'poop') 
		GM_setValue("banner_nws", 'true');
	if (GM_getValue("banner_random", 'poop') == 'poop')
		GM_setValue("banner_random", 'true');
	if (GM_getValue("banner_interesting", 'poop') == 'poop')
		GM_setValue("banner_interesting", 'true');
	if (GM_getValue("banner_political", 'poop') == 'poop')
		GM_setValue("banner_political", 'true');
	if (GM_getValue("banner_offtopic", 'poop') == 'poop')
		GM_setValue("banner_offtopic", 'true');

	function appendBannerCheckbox(bannerid, filterid, humanname) 
	{
		newcheck = document.createElement('input');
		newcheck.type = "checkbox";
		newcheck.id = bannerid;
		newlabel = document.createElement('label');
		newlabel.appendChild(newcheck);
		newlabel.innerHTML += " Banner";
		currentcheck = document.getElementById(filterid);
		if (currentcheck) 
		{
			currentcheck.parentNode.parentNode.appendChild(newlabel);
			newcheck = document.getElementById(bannerid)
			newcheck.checked = (GM_getValue(bannerid, "true") == "true");
			newcheck.addEventListener('change', toggleBanner, false);
		}
	}
	
	appendBannerCheckbox("banner_nws", "f_nws", "Not Work Safe");
	appendBannerCheckbox("banner_random", "f_random", "Stupid");
	appendBannerCheckbox("banner_interesting", "f_interesting", "Interesting");
	appendBannerCheckbox("banner_political", "f_political", "Political / Religious");
	appendBannerCheckbox("banner_offtopic", "f_offtopic", "Tangent");
	
	/* 
	Instead of injecting a pile of CSS into the page at display time, the CSS 
	is already in place thanks to shackscripts.css.  What this chunk of code does
	is update <body>'s className to basically specify which banners should be 
	shown/hidden  
	*/
	var className = new Array();   
	if (GM_getValue("banner_political", 'true') == 'true') { className.push('showPoliticalBanners'); }
	if (GM_getValue("banner_offtopic", 'true') == 'true') { className.push('showOffTopicBanners'); }
	if (GM_getValue("banner_random", 'true') == 'true')  { className.push('showStupidBanners'); }
   if (GM_getValue("banner_nws", 'true') != 'true') { className.push('hideNwsBanners'); }
   if (GM_getValue("banner_interesting", 'true') != 'true') { className.push('hideInfBanners'); }
   
   document.getElementsByTagName('body')[0].className += ' ' + className.join(' '); 

   // log execution time
    if (GM_log) 
	 {
        GM_log('category_banners.js: ' + (getTime() - scriptStartTime) + 'ms');
    }

 })();
}